package com.canbot.u05.update;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.canbot.u05.R;

/**
 * 此对话框为艾诺中同样的对话框，可以设置标题，内容，两个按钮的内容， 同时可以设置是否显示倒计时自动关闭
 *
 * @author xiaowei
 */
public class GeneralDialog extends Dialog {

        private final int COUNTDOWN = 1;

        private int mCountDown = 5;

        private String mCountDownStr = "(" + mCountDown + "s后自动消失)";

        private Context mContext;

        private String mTitleText;

        private String mLeftBtnText = null;

        private String mRightBtnText = null;

        private TextView mTV_msg;

        private String mString; // 待显示的文本信息

        private String mMsgText; // 包含倒计时显示的文本信息

        private boolean mIsShowCountdown;

        private IButtonClickListerner mClickListenerInterface;

        private int mBkBtnLeft = 0, mBkBtnRight = 0; //按钮的背景资源ID

        private final String TAG = "GeneralDialog";

        private Handler mHandler = new Handler() {
                public void handleMessage(android.os.Message msg) {
                        switch (msg.what) {
                                case COUNTDOWN:
                                        mCountDown--;
                                        if (mCountDown == 0) {
                                                cancel();
                                        }
                                        else {
                                                mMsgText = mString + "(" + mCountDown + "s后自动消失)";
                                                mTV_msg.setText(mMsgText);
                                                mHandler.sendEmptyMessageDelayed(COUNTDOWN, 1 * 1000);
                                        }
                                        break;

                                default:
                                        break;
                        }
                }

                ;
        };

        /**
         * @param context
         * @param titleText
         * @param leftBtnText
         * @param rightBtnText
         * @param msgText
         * @param isShowCountdown 是否倒计时自动关闭
         * @param IClickListener
         */
        public GeneralDialog(Context context, String titleText, String leftBtnText, String rightBtnText, String msgText,
                             boolean isShowCountdown, IButtonClickListerner IClickListener) {
                super(context, R.style.centerdialog);
                this.mContext = context;
                this.mTitleText = titleText;
                this.mString = msgText;
                this.mLeftBtnText = leftBtnText;
                this.mRightBtnText = rightBtnText;
                this.mIsShowCountdown = isShowCountdown;
                this.mMsgText = msgText + mCountDownStr;
                this.mClickListenerInterface = IClickListener;
        }


        /**
         * 按钮按照默认的显示
         *
         * @param context
         * @param titleText
         * @param msgText
         * @param isShowCountdown
         * @param cLickListenerInterface
         */
        public GeneralDialog(Context context, String titleText, String msgText, boolean isShowCountdown,
                             IButtonClickListerner cLickListenerInterface) {
                super(context, R.style.centerdialog);
                this.mContext = context;
                this.mTitleText = titleText;
                this.mString = msgText;
                this.mMsgText = msgText + mCountDownStr;
                this.mIsShowCountdown = isShowCountdown;
                this.mClickListenerInterface = cLickListenerInterface;
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                initView();
        }

        private void initView() {
                LayoutInflater layoutInflater = LayoutInflater.from(mContext);
                View viewDialog = layoutInflater.inflate(R.layout.dialog_center2, null);

                setContentView(viewDialog);
                setCanceledOnTouchOutside(false);
                TextView mTV_title = (TextView) viewDialog.findViewById(R.id.tv_dialogtitle);
                mTV_title.setText(mTitleText);
                Button mBtnLeft = (Button) viewDialog.findViewById(R.id.btn_dialogleft);
                Button mBtnRight = (Button) viewDialog.findViewById(R.id.btn_dialogright);
                mTV_msg = (TextView) findViewById(R.id.tv_dialogmsg);
                if (mIsShowCountdown) {
                        mTV_msg.setText(mMsgText);
                }
                else {
                        mTV_msg.setText(mString);
                }
                if (mRightBtnText == null) {
                        mBtnRight.setText("确定");
                }
                else {
                        mBtnRight.setText(mRightBtnText);
                }

                if (mLeftBtnText == null) {
                        mBtnLeft.setText("取消");
                }
                else {
                        mBtnLeft.setText(mLeftBtnText);
                }
                if (mBkBtnLeft != 0) {
                        mBtnLeft.setBackgroundResource(mBkBtnLeft);
                }
                if (mBkBtnRight != 0) {
                        mBtnRight.setBackgroundResource(mBkBtnRight);
                }
                mBtnLeft.setOnClickListener(new ClickListener());
                mBtnRight.setOnClickListener(new ClickListener());
        }


        @Override
        public void show() {
                super.show();
                if (mIsShowCountdown) {
                        mHandler.sendEmptyMessageDelayed(COUNTDOWN, 1 * 1000);
                }
        }

        @Override
        public void cancel() {
                super.cancel();
                if (mIsShowCountdown) {
                        mHandler.removeMessages(COUNTDOWN);
                }
        }

        private class ClickListener implements android.view.View.OnClickListener {

                @Override
                public void onClick(View v) {
                        cancel();
                        int viewID = v.getId();
                        switch (viewID) {
                                case R.id.btn_dialogright:
                                        if (mClickListenerInterface != null) {
                                                mClickListenerInterface.doBtnRight();
                                        }
                                        break;
                                case R.id.btn_dialogleft:
                                        if (mClickListenerInterface != null) {
                                                mClickListenerInterface.doBtnLeft();
                                        }
                                        break;
                                default:
                                        break;
                        }
                }

        }


        public static interface IButtonClickListerner {

                public void doBtnRight();

                public void doBtnLeft();
        }

}
